<? // lib

// database - - - - - - - - - - - - - - - - - - - -
$_db = mysql_connect( $_SERVER['REDIRECT_addr'], $_SERVER['REDIRECT_user'], $_SERVER['REDIRECT_pwd'] );
unset( $_SERVER['REDIRECT_addr'] );
unset( $_SERVER['REDIRECT_user'] );
unset( $_SERVER['REDIRECT_pwd'] );
if( !$_db ) ajax_exit( 'error', 'internal: cannot connect to db' );
mysql_select_db( $_SERVER['REDIRECT_db'] );
// todo: error on db or schema

//-$rcd = db_get_rcd( "select name from users where usr_id=1" );
//-echo $rcd['name'];
//-exit;

// session - - - - - - - - - - - - - - - - - - - -
session_start();

// initial session
if( !count( $_SESSION ) ) $_SESSION = array(
	'usr_id'	=> null,	// id into users table
	'login'		=> null,	// users.name
	'name'		=> null,	// users.login
	'login_ts'	=> null,	// when logged in - null if not
	'prv_access_ts'	=> null,	// previous access ts
	'cur_access_ts'	=> null,	// this access ts
	'prv_uri'	=> null,	// previous uri
	'cur_uri'	=> null,	// current uri
	'prv_hash'	=> null,	// previous hash
	'cur_hash'	=> mt_rand( 0, 1073741823 ),	// current hash (from client if first access)
	//='cur_hash'	=> 123456789,	// current hash (from client if first access)
	'pgn_url'	=> null );	// url source of pgn
$_SESSION['prv_access_ts'] = $_SESSION['cur_access_ts'];
$_SESSION['cur_access_ts'] = time();
$_SESSION['prv_uri'] = $_SESSION['cur_uri'];
$_SESSION['cur_uri'] = $_SERVER['REQUEST_URI'];

// save access to db
if( $_SESSION['usr_id'] ) db_dml( "update users set access_ct=access_ct+1, access_ts=current_timestamp where usr_id={$_SESSION['usr_id']}" );


// utility functions - - - - - - - - - - - - - - - - - - - -

// ajax error - error status and exit for ajax calls
function ajax_exit( $status, $msg, $arr=null ) {
	$jason_arr = array( 'status'=>$status, 'message'=>$msg );
	$_SESSION['prv_hash'] = $_SESSION['cur_hash'];
	$_SESSION['cur_hash'] = mt_rand( 0, 1073741823 );
	//=$_SESSION['cur_hash'] = 123456789;
	$jason_arr['hash'] = $_SESSION['cur_hash'];
	if( $arr ) foreach( $arr as $key=>$val ) $jason_arr[$key] = $val;
	echo json_encode( $jason_arr );
	exit;
}


function db_get_rcd( $query ) {
GLOBAL $_db;
	$result = mysql_query( $query );
	// for now do not catch errors - explode
	if( !$result ) ajax_exit( 'error', "db_get_rcd( '".$query.'" )' );
	if( !mysql_num_rows($result) ) return array();
	return mysql_fetch_assoc( $result );
}
function db_get_rcds( $query ) {
GLOBAL $_db;
	$arr = array();
	$result = mysql_query( $query );
	// for now do not catch errors - explode
	if( !$result ) ajax_exit( 'error', "db_get_rcd( '".$query.'" )' );
	while( $rcd = mysql_fetch_assoc( $result ) ) $arr[] = $rcd;
	return $arr;
}
function db_get_col( $query ) {
GLOBAL $_db;
	$result = mysql_query( $query );
	// for now do not catch errors - explode
	if( !mysql_num_rows($result) ) return array();
	$row = mysql_fetch_array( $result );
	return $row[0];
}
function db_dml( $query ) {
GLOBAL $_db;
	$result = mysql_query( $query );
	return mysql_affected_rows( $_db );
}


// pure funcs
function pure_code( $str ) {
	$str = strToLower($str);
	preg_match( '/^([a-z0-9_]+)/', $str, $match_arr );
	return ( $match_arr[1] ) ? $match_arr[1] : null;
}
function pure_email( $str ) {
	// ((non-chars|^)(chars@chars)(non-chars|$))[2]
	return $str;
}
function pure_url( $str ) {
	// make sure begin with http, https, or ftp - right?
	return $str;
}
function pure_string( $str ) {
	return mysql_real_escape_string($str);
}
function pure_integer( $str ) {
	return (int)$str;
}


?>
